from functools import lru_cache
from typing import Type

from pydantic import AnyUrl, Field
from pydantic_settings import BaseSettings


# Creating the Settings only once with lru_cache
@lru_cache()
def get_settings(settings_type: Type[BaseSettings]):
    return settings_type()


class AppSettings(BaseSettings):
    name: str = Field(default="Anonymizer")

    class Config:
        env_prefix = "APP_"


class KafkaSettings(BaseSettings):
    sasl_mechanism: str
    security_protocol: str
    boostrap_server: str
    sasl_username: str
    sasl_password: str
    group_id: str

    class Config:
        env_prefix = "KAFKA_"


class S3Settings(BaseSettings):
    region_name: str = "us-west-2"
    url: AnyUrl = "https://s3.xplogene.com"  # type: ignore
    access_key: str
    secret_access_key: str
    bucket: str = "testdata"

    class Config:
        env_prefix = "S3_"


class RetrySettings(BaseSettings):
    wait_min: float = 0.1
    wait_max: float = 0.6
    attempts: int = 3

    class Config:
        env_prefix = "RETRY_"


class PostgreSQLSettings(BaseSettings):
    user: str
    password: str
    host: str
    port: str
    db: str

    class Config:
        env_preifx = "POSTGRESQL_"
